package com.opencv.controller;

import com.opencv.domain.ResponseMsg;
import com.opencv.thread.AsyncTask;
import com.opencv.thread.LiteTask;
import com.opencv.thread.ThreadPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author qz.wu
 * @date 2022/1/20 16:07
 * @descriptions TODO
 */
@RestController
@RequestMapping("/lite/task")
@Api(tags = "任务批处理测试")
public class ListTaskController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ListTaskController.class);

    @GetMapping("/batch/async")
    @ApiOperation("批量执行异步任务测试")
    public ResponseMsg<Boolean> batchStartAsyncTask() {
        List<Integer> jobData = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            jobData.add(i);
        }
        int partShardSize = 10;

        AsyncTask<Integer> asyncTask = (list) -> {
            list.stream().forEach(job -> {
                Integer result = job + 1000;
                LOGGER.info("执行异步任务[{}], 处理结果[{}]", job, result);
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        };
        LiteTask.executeAsyncTask(jobData, partShardSize, asyncTask);

        return ResponseMsg.success(Boolean.TRUE);
    }

    private void batchAsyncTaskJob(List<Integer> jobData) {
        jobData.stream().forEach(job -> {
            Integer result = job + 1000;
            LOGGER.info("执行异步任务[{}], 处理结果[{}]", job, result);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    @GetMapping("/batch/sync")
    @ApiOperation("批量执行异步任务测试")
    public ResponseMsg<Boolean> batchStartSyncTask() {
        List<Integer> jobData = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            jobData.add(i);
        }
        int partShardSize = 10;

        List<String> resultList = LiteTask.executeSyncTask(jobData, partShardSize, jobDataList -> this.batchSyncTaskJob(jobDataList));

        return ResponseMsg.success(Boolean.TRUE);
    }


    private List<String> batchSyncTaskJob(List<Integer> jobData) {
        List<String> resultList = new ArrayList<>();
        jobData.stream().forEach(job -> {
            Integer result = job + 1000;
            LOGGER.info("执行同步任务[{}], 处理结果[{}]", job, result);
            resultList.add(String.format("执行同步任务:" + job));
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return resultList;
    }

    @GetMapping("/async/result")
    @ApiOperation("获取异步任务的线程池的信息")
    public ResponseMsg<Boolean> getAsyncThreadPoolInfo() {
        ThreadPool.printThreadPoolStatus(ThreadPool.getAsyncTaskPool());
        return ResponseMsg.success(Boolean.TRUE);
    }

    @GetMapping("/sync/result")
    @ApiOperation("获取同步任务的线程池的信息")
    public ResponseMsg<Boolean> getSyncThreadPoolInfo() {
        ThreadPool.printThreadPoolStatus(ThreadPool.getSyncTaskPool());
        return ResponseMsg.success(Boolean.TRUE);
    }
}
